﻿using System;
using System.IO;
using System.Net;
using KPI.Data;
using KPI.Entity;
using TYBase;

namespace KPI.Api
{
    public class FileApi
    {
        public static R<Tbl_File> UploadFile(string filePath, EnumFileTypes fileType, string bucketName)
        {
            try
            {
                var route = new
                {
                    bucketName = bucketName,
                    fileType = (int)fileType,
                };
                LocalHttp http = new LocalHttp();
                if (!http.SetBaseUri(KPIData.ServerURL)
                    .SetUrl($"/file/uploadFile?{route.AsQueryData()}")
                    .SetPost()
                    .UploadFile(filePath, out string data))
                {
                    return R<Tbl_File>.Fail(data);
                }
                return data.AsModel<R<Tbl_File>>();
            }
            catch (Exception ex)
            {
                return R<Tbl_File>.Fail("创建失败!", ex);
            }
        }

        public static R<object> Download(string filePath, string bucketName, string fileRealName)
        {
            try
            {
                string url = $"{KPIData.ServerURL}/file/downloadFile?bucketName={bucketName}&fileRealName={fileRealName}";
                using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    // 设置参数
                    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                    request.Timeout = 30000;
                    request.ReadWriteTimeout = 5000;
                    //发送请求并获取相应回应数据
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    Stream responseStream = response.GetResponseStream();
                    //创建本地文件写入流
                    byte[] bArr = new byte[1024];
                    int size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    while (size > 0)
                    {
                        fileStream.Write(bArr, 0, size);
                        size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    }
                    responseStream.Close();
                    return R<object>.OK();
                }
            }
            catch (Exception ex)
            {
                return R<object>.Fail("下载文件失败!", ex);
            }
        }

        public static R<Tbl_File> GetFile(string uuid)
        {
            try
            {
                LocalHttp http = new LocalHttp();
                if (!http.SetBaseUri(KPIData.ServerURL)
                    .SetGet()
                    .SetUrl($"/file/getFile?uuid={uuid}")
                    .Excute(out string data))
                {
                    return R<Tbl_File>.Fail(data);
                }
                return data.AsModel<R<Tbl_File>>();
            }
            catch (Exception ex)
            {
                return R<Tbl_File>.Fail("获取失败!", ex);
            }
        }

    }
}
